/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#ifndef CONFIGWIZARD_H
#define CONFIGWIZARD_H

#include "include/raptor.h"

#include <QWizard>
#include <QtGui>
#include <QPointer>
#include <QNetworkProxy>
#include <QHttp>
#include <QGridLayout>
#include <QSettings>
#include <QMessageBox>

class configFolderPage;
class configXmlPage;
class configNetworkPage;

//class confiWizard_exception: public std::exception {};

class configWizard : public QWizard
{
    Q_OBJECT

    public:
        enum { PAGE_FOLDERS, PAGE_NETWORK, PAGE_XML };

        configWizard(QWidget *parent = 0);
        ~configWizard();

    private:

        QPointer<configFolderPage> folderPage;
        QPointer<configNetworkPage> networkPage;
        QPointer<configXmlPage> xmlPage;

        bool validateCurrentPage();
        void saveToQSettings();

        QString raptor;
        QString downloads;

        QString host;
        quint16 port;
        QString user;
        QString pass;
        QNetworkProxy::ProxyType type;

        bool useSSL;

        QStringList hoster;
        QStringList container;
};

class configFolderPage : public QWizardPage
{
    Q_OBJECT

    public:
        configFolderPage();
        ~configFolderPage();

        const QString raptor() const {return raptorLineEdit->text();}
        const QString downloads() const {return downloadsLineEdit->text();}

    private:
        QPointer<QGridLayout> mainLayout;

        QPointer<QLabel> raptorLabel;
        QPointer<QLabel> downloadsLabel;

        QPointer<QLineEdit> raptorLineEdit;
        QPointer<QLineEdit> downloadsLineEdit;

        QPointer<QPushButton> raptorPushButton;
        QPointer<QPushButton> downloadsPushButton;

    private slots:

        void slot_raptorPushButton_clicked();
        void slot_downloadsPushButton_clicked();
};

class configNetworkPage : public QWizardPage
{
    Q_OBJECT

    public:
        configNetworkPage();
        ~configNetworkPage();

        const QString host() const {return proxyHostLineEdit->text();}
        quint16 port() const {return proxyPortSpinBox->value();}
        QNetworkProxy::ProxyType type() const {return QNetworkProxy::ProxyType(proxyTypeComboBox->itemData(proxyTypeComboBox->currentIndex()).toInt());}
        const QString user() const {return proxyUserLineEdit->text();}
        const QString pass() const {return proxyPassLineEdit->text();}
        bool useSSL() const {return sslCheckBox->isChecked(); }

    private:
        QPointer<QGridLayout> mainLayout;

        QPointer<QLabel> proxy1Label;
        QPointer<QLabel> proxy2Label;

        QPointer<QSpinBox> proxyPortSpinBox;
        QPointer<QComboBox> proxyTypeComboBox;

        QPointer<QLineEdit> proxyHostLineEdit;
        QPointer<QLineEdit> proxyUserLineEdit;
        QPointer<QLineEdit> proxyPassLineEdit;

        QPointer<QCheckBox> sslCheckBox;

    private slots:
};

class configXmlPage : public QWizardPage
{
    Q_OBJECT

    public:
        configXmlPage(bool const& useSSL=true);
        ~configXmlPage();

        const QPointer<QStandardItemModel> hoster() const {return hosterSandardItemModel;}
        const QPointer<QStandardItemModel> container() const {return containerSandardItemModel;}

        void setUseSSL(bool const& use);
        int setProxy(QNetworkProxy proxy);

    private:

        enum E_FILESTATE
        {
            STATE_WAITING,
            STATE_DONE,
            STATE_TIMEOUT,
            STATE_ERROR,
        };

        enum E_TYPE
        {
            TYPE_HOSTER,
            TYPE_CONTAINER,
        };

        QHttp http;

        QPointer<QGridLayout> mainLayout;

        QPointer<QTreeView> hosterTreeView;
        QPointer<QTreeView> containerTreeView;

        QPointer<QLabel> hosterLabel;
        QPointer<QLabel> containerLabel;

        QPointer<QStandardItemModel> hosterSandardItemModel;
        QPointer<QStandardItemModel> containerSandardItemModel;

        QUrl raptorUrl;
        int timeout;

        QByteArray response;
        E_FILESTATE state;
        int waitingId;

        QByteArray getPostArray(QString const& action);
        QStringList getXmlName(E_TYPE const& type);
        void initializePage();

    private slots:

        void slot_http_requestFinished(int requestId, bool httpError);
        void slot_http_readyRead(const QHttpResponseHeader & resp);
        void slot_http_sslErrors(const QList<QSslError> &errors);
};

#endif // CONFIGWIZARD_H
